#include "SequenceGuiFactory.h"
#include "QWidget"

#include "ActionListView.h"
#include "ActionListPainter.h"

#include "QPushButton"
#include "QHBoxLayout"
#include "QVBoxLayout"
#include "QCommonStyle"

#include "IF_SequenceEngineInterface.h"
#include "SequenceModel.h"
#include "sequencerunner.h"
#include "SequenceItemDelegate.h"
#include "SequencePlaybar.h"
#include "SequencePainter.h"
#include "SequenceControlPanel.h"
#include "QtTimerService.h"


SequenceGuiFactory::SequenceGuiFactory(IF_SequenceEngineInterface *engineIF,
                                       SequenceModel *sequenceModel,
                                       QWidget *parent) :
   QObject(parent),
   m_engineInterface(engineIF),
   m_sequenceModel(sequenceModel),
   m_container(parent)
{
}

void SequenceGuiFactory::build( ActionListController *actionController, SequenceRunner *sequenceRunner,
                                QAbstractItemModel *mediaModel, QAbstractItemModel *lightModel)
{
   Q_ASSERT( actionController != NULL);
   QWidget *toolbarArea = new QWidget( m_container);

   ActionListView *eventListView = new ActionListView( *actionController,
                                                       new SequencePainter(), m_container);

   eventListView->setModel( m_sequenceModel);
   eventListView->setItemDelegate( new SequenceItemDelegate( lightModel, mediaModel, this));

   SequencePlaybar *playbar = new SequencePlaybar( *actionController, *sequenceRunner,
                                                   new QtTimerService(this), eventListView);
   eventListView->setActionBar( playbar);

   SequenceControlPanel *sequencePanel = new SequenceControlPanel( *eventListView, *actionController,
                                                                   *m_sequenceModel, this);
   connect( playbar, SIGNAL(editRequest()), sequencePanel, SLOT(onEditRequest()) );
   connect( playbar, SIGNAL(deleteRequest()), sequencePanel, SLOT(onDeleteRequest()) );

   m_container->setLayout( new QVBoxLayout());
   toolbarArea->setLayout( new QHBoxLayout());

   m_container->layout()->setMargin(2);
   m_container->layout()->addWidget( toolbarArea);
   m_container->layout()->addWidget( eventListView);

   QCommonStyle style;

   /* 'play sequence' button */
   QPushButton *play = new QPushButton( style.standardIcon( QStyle::SP_MediaPlay),
                                           QString(), toolbarArea);
   play->setToolTip( tr("play active item of the sequence"));
   play->setFocusPolicy(Qt::NoFocus);
   toolbarArea->layout()->addWidget( play);

   /* 'pause sequence' button */
   QPushButton *pause = new QPushButton( style.standardIcon( QStyle::SP_MediaPause),
                                           QString(), toolbarArea);
   pause->setToolTip( tr("pause the sequence"));
   pause->setFocusPolicy(Qt::NoFocus);
   pause->setEnabled( false);
   toolbarArea->layout()->addWidget( pause);

   toolbarArea->layout()->addItem( new QSpacerItem( 1,1, QSizePolicy::Expanding));

   /* 'new sequence item' button */
   QPushButton *newItem = new QPushButton( style.standardIcon( QStyle::SP_FileDialogNewFolder),
                                           QString(), toolbarArea);
   newItem->setToolTip( tr("create a new sequence item"));
   newItem->setFocusPolicy(Qt::NoFocus);
   toolbarArea->layout()->addWidget( newItem);

   /* 'move up' button */
   QPushButton *moveUp = new QPushButton( style.standardIcon( QStyle::SP_ArrowUp),
                                           QString(), toolbarArea);
   moveUp->setToolTip( tr("move focused item up"));
   moveUp->setFocusPolicy(Qt::NoFocus);
   toolbarArea->layout()->addWidget( moveUp);

   /* 'move down' button */
   QPushButton *moveDown = new QPushButton( style.standardIcon( QStyle::SP_ArrowDown),
                                           QString(), toolbarArea);
   moveDown->setToolTip( tr("move focused item down"));
   moveDown->setFocusPolicy(Qt::NoFocus);
   toolbarArea->layout()->addWidget( moveDown);

   /* connection to interface */
   connect( newItem, SIGNAL(clicked()), m_sequenceModel, SLOT(createEmptyItem()) );
   connect( play, SIGNAL(clicked()), m_engineInterface, SLOT(play()) );
   connect( pause, SIGNAL(clicked()), m_engineInterface, SLOT(pause()) );

   connect( moveUp, SIGNAL(clicked()), eventListView, SLOT(shiftFocusedUp()) );
   connect( moveDown, SIGNAL(clicked()), eventListView, SLOT(shiftFocusedDown()) );

   connect( m_engineInterface, SIGNAL(sequenceIsRunning(bool)), play, SLOT(setDisabled(bool)) );
   connect( m_engineInterface, SIGNAL(sequenceIsRunning(bool)), pause, SLOT(setEnabled(bool)) );

   connect( m_engineInterface, SIGNAL(sequenceError(QString)),
            eventListView, SLOT(displayError(QString)) );
}
